/**
 * hnjz.com Inc.
 * Copyright (c) 2004-2014 All Rights Reserved.
 */
package com.hnjz.common.dao;

/**
 * oracle数据库方言（分页）的实现
 * 
 * @author wumi
 * @version $Id: OracleDialect.java, v 0.1 Jan 24, 2014 10:51:06 AM wumi Exp $
 */
public class OracleDialect extends Dialect {

    /**
     * @see com.hnjz.common.dao.Dialect#supportsLimit()
     */
    public boolean supportsLimit() {
        return true;
    }

    /**
     * @see com.hnjz.common.dao.Dialect#supportsLimitOffset()
     */
    public boolean supportsLimitOffset() {
        return this.supportsLimit();
    }

    /**
     * @see com.hnjz.common.dao.Dialect#getLimitString(java.lang.String, int, int)
     */
    public String getLimitString(String sql, int offset, int limit) {

        sql = sql.trim();
        boolean isForUpdate = false;
        if (sql.toLowerCase().endsWith(" for update")) {
            sql = sql.substring(0, sql.length() - 11);
            isForUpdate = true;
        }

        StringBuilder pagingSelect = new StringBuilder(sql.length() + 100);
        pagingSelect.append("select * from ( select row_.*, rownum rownum_ from ( ");
        pagingSelect.append(sql);
        pagingSelect.append(" ) row_ where rownum <= ");
        pagingSelect.append(offset+limit);
        pagingSelect.append(") where rownum_ > ");
        pagingSelect.append(offset);
        if (isForUpdate) {
            pagingSelect.append(" for update");
        }
        return pagingSelect.toString();
    }

}
